$(function() {
	ajax();
	
		//修改公司开始
		$('#company-update').click(function() {
		
			//1.进行传输参数到后台开始
			$.ajax({
				type: "post",
				url: getPath() + "/company",
				data: ({
					"method": "updateProductMessage",
					"id":$('#update-hidden').val(),
					"companyTel": $('input[name="companyTel"]').val(),
					"companyManager": $('input[name="companyManager"]').val(),
					"companyName": $('input[name="companyName"]').val(),
				}),
				dataType: "json",
				success: function(jsonData) {
					if(jsonData.result == 'success') {
						$('#success-message').html(jsonData.message);
						$('#success-message').attr('class', 'alert alert-success alert-dismissable');
						$('#success').modal('show');
						goQueryPage('company-list');
					}else{
						$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
						$('#error-message').html(jsonData.message);
						$('#error').modal('show');
						popHide("error");
					}
				}
			});
			//1.进行传输参数到后台结束
		});
		//修改公司结束
	
		//反选的模块开始
		$('#check-all').bind('click', function() {
			var list = $('input[type="checkbox"]');
			list.each(function(index, data) {
				if(index != 0) {
					if(data.checked) {
						data.checked = false;
					} else {
						data.checked = true;
					}
				}
			});
		});
		//反选的模块结束
	
		//删除公司开始
		$('#open-company-delete').click('click', function() {
	
			//1.获取到需要删除的id开始
			var checkedList = $('input[type="checkbox"]:checked');
			var companyIds = "";
			for(var i = 0; i < checkedList.length; i++) {
				if(checkedList[i].value != 'on') {
					companyIds = companyIds + checkedList[i].value + ",";
				}
			}
			companyIds = companyIds.substring(0, companyIds.length - 1);
			//1.获取到需要删除的id结束
	
			$.ajax({
				type: "post",
				url: getPath() + "/company",
				data: ({
					"method": "batchDeleteIds",
					"companyIds": companyIds,
				}),
				dataType: "json",
				success: function(jsonData) {
					if(jsonData.result == 'success') {
						$('#success-message').html(jsonData.message);
						$('#success-message').attr('class', 'alert alert-success alert-dismissable');
						$('#success').modal('show');
						goQueryPage('company-list');
					} else {
						$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
						$('#error-message').html(jsonData.message);
						$('#error').modal('show');
						popHide("error");
					}
				}
			});
		});
		//删除公司结束
	
		//增加公司开始
		$('#company-add').click(function() {
			//1.进行传输参数到后台开始
			$.ajax({
				type: "post",
				url: getPath() + "/company",
				data: ({
					"method": "add",
					"companyTel": $('input[name="companyTel"]').val(),
					"companyManager": $('input[name="companyManager"]').val(),
					"companyName": $('input[name="companyName"]').val(),
				}),
				dataType: "json",
				success: function(jsonData) {
					if(jsonData.result == 'success') {
						$('#success-message').html(jsonData.message);
						$('#success-message').attr('class', 'alert alert-success alert-dismissable');
						$('#success').modal('show');
						goQueryPage('company-list');
					} else {
						$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
						$('#error-message').html(jsonData.message);
						$('#error').modal('show');
						popHide("error");
					}
				}
			});
			//1.进行传输参数到后台结束
		});
		//增加公司结束
	
		//关闭模态框调回查询页面开始
		$('#company-add-modal,#company-update-modal').on('hide.bs.modal', function() {
			goQueryPage('over-modal');
		});
		//关闭模态框调回查询页面结束
});

//查询全部公司数据开始
function ajax() {
	$('#tbody').empty();
	$.ajax({
		type: "post",
		url: getPath() + "/company",
		data: ({
			"method": "findAll",
		}),
		dataType: "json",
		success: function(jsonData) {
			$(jsonData.data).each(function(index, data) {
				var tr = $('<tr class="info"></tr>');
				var check = $('<td><input type="checkbox" value="' + data.id + '" /></td>');
				var companyName = $('<td>' + data.companyName + '</td>');
				var companyTel = $('<td>' + data.companyTel + '</td>');
				var companyManager = $('<td>' + data.companyManager + '</td>');

				tr.append(check);
				tr.append(companyName);
				//tr.append(sortName);
				tr.append(companyTel);
				tr.append(companyManager);

				$('#tbody').append(tr);
			});
		}
	});
}
//查询全部公司数据结束

//自动关闭窗口开始
function popHide(object) {
	setTimeout(function() {
		$('#' + object).modal('hide');
	}, 1500);
}
//自动关闭窗口结束

//跳转到查询页面开始
function goQueryPage(page) {
	if(page === "over-modal"){
		ajax();
		window.location.href =  "company-list.html";
		return;
	}
	
	//$('#company-add-modal,#company-update-modal').modal('hide');
	setTimeout(function() {
		popHide("success");
		ajax();
		window.location.href = page + ".html";
	}, 2000);
}
//跳转到查询页面结束